import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import Ridge
from sklearn.metrics import r2_score
import matplotlib
matplotlib.use('TkAgg')

x = [4, 8, 9, 8, 7, 12, 6, 10, 6, 9]
y = [9, 18, 21, 15, 17, 22, 16, 25, 12, 22]
x0 = 100
x, y = np.array(x).reshape((-1, 1)), np.array(y)

print(x)

lineback = Ridge(alpha=1.0)
lineback.fit(x, y)
y0 = lineback.predict(np.array([x0]).reshape((-1, 1)))[0]
print(f'系数k: {lineback.coef_[0]}，截距b: {lineback.intercept_}')
print(f'预测当X为{x0}时，Y的值为：{y0}')
print(f'R方值：{r2_score(y, lineback.predict(x))}')

plt.scatter(x, y)
plt.show()
